package com.longnows.appserver.api.v1;


import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.longnows.appserver.exception.http.NotFoundException;
import com.longnows.appserver.model.Theme;
import com.longnows.appserver.service.ThemeService;
import com.longnows.appserver.vo.ThemePureVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/v1/theme")
@Validated
public class ThemeController {

    @Autowired
    private ThemeService themeService;

    @GetMapping("/by/names")
    public List<ThemePureVo> getThemeGroupByNames(@RequestParam(name = "names") String names){

        List<String> nameList = Arrays.asList(names.split(","));
        List<Theme> themes = themeService.findByNames(nameList);
        List<ThemePureVo> list= new ArrayList<>();
        themes.forEach(theme -> {
            Mapper mapper = DozerBeanMapperBuilder.buildDefault();
            ThemePureVo vo = mapper.map(theme,ThemePureVo.class);
            list.add(vo);
        });
        return  list;
    }

    @GetMapping("/name/{name}/with_spu")
    public Theme getThemeByNameWithSpu(@PathVariable(name = "name") String themeName){
      Optional<Theme> optionalTheme= themeService.getThemeByNameWithSpu(themeName);
        return  optionalTheme.orElseThrow(()->new NotFoundException(30009));
    }


}
